package com.liesheng.haylou.view.dialog;



import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;

import com.liesheng.haylou.R;
import com.liesheng.haylou.utils.ToastUtil;


/**
 * Date:${DATA}
 * Author ${ZhangZhiQiang}
 * Description
 */
public class DeviceDialog extends  DialogFragment implements View.OnClickListener{
    private OnDialogClickListener OnDialogClickListener;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        getDialog().setCanceledOnTouchOutside(true);
        return inflater.inflate(R.layout.device_dialog, container, false);
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        View view = getView();
        TextView tvClose = view.findViewById(R.id.tv_close);
        TextView tvSave= view.findViewById(R.id.tv_save);
        EditText etDeviceName = view.findViewById(R.id.et_deviceName);

        tvClose.setOnClickListener(this);
        tvSave.setOnClickListener(this);
        etDeviceName.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        dismiss();
        switch (v.getId()) {
            case R.id.tv_close:
                if (OnDialogClickListener != null) {
                    OnDialogClickListener.close();
                }
                break;
            case R.id.tv_save:
                if (OnDialogClickListener != null) {
                    OnDialogClickListener.toPage();
                }
                break;
            case R.id.et_deviceName:
                if (OnDialogClickListener !=null){
                    OnDialogClickListener.save();
                }
                break;
        }
    }
    public DeviceDialog show(FragmentActivity activity) {
        FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction();
        Fragment fragment = activity.getSupportFragmentManager().findFragmentByTag(this.getClass().getSimpleName());
        if (fragment != null) {
            fragmentTransaction.remove(fragment);
        }
        show(fragmentTransaction, this.getClass().getSimpleName());
        return this;
    }
    public void setOnDialogClickListener(OnDialogClickListener onDialogClickListener) {
        this.OnDialogClickListener = onDialogClickListener;
    }
    public interface OnDialogClickListener {
        void toPage();
        void close();
        void save();
    }

}
